// Copyright (c) 2025, 大连理工大学 (Dalian University of Technology)
//
// Licensed under the Mulan PSL v2.
// You can use this file according to the terms and conditions of the Mulan PSL v2.
// You may obtain a copy of the License at
//
//     http://license.coscl.org.cn/MulanPSL2
//
// THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
// EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. SEE THE MULAN PSL v2
// FOR MORE DETAILS.

/*
 * @Author: qiulin_JXGM ql1746365080@gmail.com
 * @Date: 2024-10-16 09:35:57
 * @LastEditors: qiulin 1746365080@qq.com
 * @LastEditTime: 2025-01-02 17:23:20
 * @FilePath: \SGFEM\Import\BDFImport\BulkData\Property\PBEAMParser.cpp
 */
#include "PBEAMParser.h"

#include <algorithm>
#include <cstdint>

#include <boost/algorithm/string.hpp>

#include "Utility/Logging/SGLogger.h"

#include "DBManager/DBServiceFactory.h"
#include "DBManager/IService/Input/Property/IPBEAMService.h"

#include "Import/BDFImport/BDFParserBase.h"

BULKDATA_ENTRY_PARSER_REG (PBEAM);

using namespace SG::DBManager;
using namespace SG::Import;
using namespace SG::Import::BDF;
using namespace SG::DataStructure;
using SG::DataStructure::Common::Status;

/*
// file://./../../Doc/MarkDown/BulkData/PBEAM/PBEAM.md>
*/

namespace
{
    enum PBEAMIndex : std::uint8_t
    {
        PBEAMIndex_PID  = 1,
        PBEAMIndex_MID  = 2,
        PBEAMIndex_A    = 3,
        PBEAMIndex_I1   = 4,
        PBEAMIndex_I2   = 5,
        PBEAMIndex_I12  = 6,
        PBEAMIndex_J    = 7,
        PBEAMIndex_NSM  = 8,
        PBEAMIndex_C1   = 11,
        PBEAMIndex_C2   = 12,
        PBEAMIndex_D1   = 13,
        PBEAMIndex_D2   = 14,
        PBEAMIndex_E1   = 15,
        PBEAMIndex_E2   = 16,
        PBEAMIndex_F1   = 17,
        PBEAMIndex_F2   = 18,
        PBEAMIndex_K1   = 21,
        PBEAMIndex_K2   = 22,
        PBEAMIndex_S1   = 23,
        PBEAMIndex_S2   = 24,
        PBEAMIndex_NSIA = 25,
        PBEAMIndex_NSIB = 26,
        PBEAMIndex_CWA  = 27,
        PBEAMIndex_CWB  = 28,
        PBEAMIndex_M1A  = 31,
        PBEAMIndex_M2A  = 32,
        PBEAMIndex_M1B  = 33,
        PBEAMIndex_M2B  = 34,
        PBEAMIndex_N1A  = 35,
        PBEAMIndex_N2A  = 36,
        PBEAMIndex_N1B  = 37,
        PBEAMIndex_N2B  = 38,
    };

    // 解析中间变截面行
    enum PBEAMVCSIndex : std::uint8_t
    {
        PBEAMVCSIndex_SO  = 1,
        PBEAMVCSIndex_XXB = 2,
        PBEAMVCSIndex_A   = 3,
        PBEAMVCSIndex_I1  = 4,
        PBEAMVCSIndex_I2  = 5,
        PBEAMVCSIndex_I12 = 6,
        PBEAMVCSIndex_J   = 7,
        PBEAMVCSIndex_NSM = 8,
        PBEAMVCSIndex_C1  = 11,
        PBEAMVCSIndex_C2  = 12,
        PBEAMVCSIndex_D1  = 13,
        PBEAMVCSIndex_D2  = 14,
        PBEAMVCSIndex_E1  = 15,
        PBEAMVCSIndex_E2  = 16,
        PBEAMVCSIndex_F1  = 17,
        PBEAMVCSIndex_F2  = 18,
    };

    enum SOSTATUS : std::uint8_t
    {
        SOSTATUS_UNKnow = 0,  ///< 未定义
        SOSTATUS_YES    = 1,  ///< 应力输出
        SOSTATUS_YESA   = 2,  ///< 应力输出同A
        SOSTATUS_NO     = 3,  ///< 不执行应力输出
    };

    struct IntermediateData
    {
        double   xbKey    = 0.0;              ///< 截面位置
        size_t   rowIndex = 0;                ///< 截面所在行号
        size_t   rowSize  = 0;                ///< 截面所占行数
        SOSTATUS soType   = SOSTATUS_UNKnow;  ///< 截面类型
    };

    /**
     * @brief 汇总处理输入数据，根据SO字段和XB字段的信息生成中间数据的信息，以及起始与结尾的行数。
     *
     * @param dividedEntryInfo 输入的数据分割后的字符串信息，包含每行数据的字段。
     * @param rowSize 输入数据的总行数。
     * @param[out] IntermediateDataSumInfo 用于存储中间数据的总结信息的map，键是XB字段的数值，值是中间数据结构。
     * @param[out] beginGeneralRowSize 更新开始处理的行数目。
     * @param[out] endGeneralRowSize 更新处理结束的行数目。
     */
    void summarizeData (const std::vector<std::string>&          dividedEntryInfo,
                        const size_t&                            rowSize,
                        std::map<double, IntermediateData>& _OUT IntermediateDataSumInfo,
                        size_t& _OUT                             beginGeneralRowSize,
                        size_t& _OUT                             endGeneralRowSize)
    {
        // 初始化行数量
        beginGeneralRowSize = endGeneralRowSize = 0;

        // 中间数据的临时变量
        IntermediateData tempIntermediateData;

        // 遍历每一行数据
        for (size_t curRowIndex = 0; curRowIndex < rowSize; curRowIndex++)
        {
            // 获取SO字段的值
            std::string soStrKey = dividedEntryInfo[curRowIndex * 10 + 1];
            // 获取XB字段的值
            std::string xbStrKey = dividedEntryInfo[curRowIndex * 10 + 2];

            // 去除SO字段的前后空白字符
            boost::trim (soStrKey);

            // 根据SO字段的值判断如何解析中间数据
            if ("NO" == soStrKey)
            {
                tempIntermediateData.soType  = SOSTATUS_NO;
                tempIntermediateData.rowSize = 1;
            }
            else if ("YESA" == soStrKey)
            {
                tempIntermediateData.soType  = SOSTATUS_YESA;
                tempIntermediateData.rowSize = 1;
            }
            else if ("YES" == soStrKey)
            {
                tempIntermediateData.soType  = SOSTATUS_YES;
                tempIntermediateData.rowSize = 2;
            }

            // 根据SO类型进行处理
            switch (tempIntermediateData.soType)
            {
            case SOSTATUS_NO:
            case SOSTATUS_YES:
            case SOSTATUS_YESA:
                // 如果这是第一次处理，记录开始行的数目
                if (beginGeneralRowSize == 0)
                {
                    beginGeneralRowSize = curRowIndex;
                }

                // 记录当前行索引和XB字段的数值
                tempIntermediateData.rowIndex = curRowIndex;
                tempIntermediateData.xbKey    = convertToReal (xbStrKey);

                // 将中间数据插入总结信息中
                IntermediateDataSumInfo.insert (std::make_pair (tempIntermediateData.xbKey, tempIntermediateData));

                // 更新结束行的索引
                endGeneralRowSize = rowSize - curRowIndex - tempIntermediateData.rowSize;

                // 重置SO状态为未知，准备处理下一行
                tempIntermediateData.soType = SOSTATUS_UNKnow;
                break;
            default:
                // 如果SO字段不匹配已知类型，跳过当前行
                break;
            }
        }
        if (IntermediateDataSumInfo.size () == 0)
        {
            beginGeneralRowSize = 2;
            endGeneralRowSize   = rowSize > 2 ? rowSize - 2 : 0;
        }
    }

    /**
     * @brief 解析一般数据，根据指定的开始行和结束行的信息，解析字段并填充到PBEAMData结构体中。
     *
     * @param dividedEntryInfo 输入的分割后的字符串数据，包含每行的字段信息。
     * @param beginRowSize 数据解析的开始行数。
     * @param endRowSize 数据解析的结束行数。
     * @param tempData 用于存储解析后的数据的PBEAMData结构体。
     */
    void parseGeneralData (std::vector<std::string>&          dividedEntryInfo,
                           size_t                             beginRowSize,
                           size_t                             endRowSize,
                           SG::DataStructure::FEM::PBEAMData& tempData)
    {
        size_t judgeEnum = 0;  // 用于与枚举值做对比的变量

        // 解析开始行的数据
        for (size_t curRowIndex = 0; curRowIndex < beginRowSize; curRowIndex++)
        {
            // 遍历每一行的字段
            for (int fieldsColumn = 1; fieldsColumn < 9; fieldsColumn++)
            {
                judgeEnum              = curRowIndex * 10 + fieldsColumn;  // 计算当前枚举值
                const size_t curIndex  = curRowIndex * 10 + fieldsColumn;  // 获取当前字段的索引
                std::string& curStrKey = dividedEntryInfo[curIndex];       // 当前字段的值
                bool const   isEmpty   = is_all_space (curStrKey);

                if (isEmpty)
                {
                    continue;  // 如果字段为空，跳过该字段
                }

                // 将当前字段转换为科学计数法表示

                // 根据枚举值解析字段并填充到tempData结构体中
                switch (judgeEnum)
                {
                case PBEAMIndex_PID:
                    tempData.m_id = convertToI64 (curStrKey);
                    break;
                case PBEAMIndex_MID:
                    tempData.m_mId = convertToI64 (curStrKey);
                    break;
                case PBEAMIndex_A:
                    tempData.m_A[0] = convertToReal (curStrKey);
                    break;
                case PBEAMIndex_I1:
                    tempData.m_I1[0] = convertToReal (curStrKey);
                    break;
                case PBEAMIndex_I2:
                    tempData.m_I2[0] = convertToReal (curStrKey);
                    break;
                case PBEAMIndex_I12:
                    tempData.m_I12[0] = convertToReal (curStrKey);
                    break;
                case PBEAMIndex_J:
                    tempData.m_J[0] = convertToReal (curStrKey);
                    break;
                case PBEAMIndex_NSM:
                    tempData.m_NSM[0] = convertToReal (curStrKey);
                    break;
                case PBEAMIndex_C1:
                    tempData.m_C1[0] = convertToReal (curStrKey);
                    break;
                case PBEAMIndex_C2:
                    tempData.m_C2[0] = convertToReal (curStrKey);
                    break;
                case PBEAMIndex_D1:
                    tempData.m_D1[0] = convertToReal (curStrKey);
                    break;
                case PBEAMIndex_D2:
                    tempData.m_D2[0] = convertToReal (curStrKey);
                    break;
                case PBEAMIndex_E1:
                    tempData.m_E1[0] = convertToReal (curStrKey);
                    break;
                case PBEAMIndex_E2:
                    tempData.m_E2[0] = convertToReal (curStrKey);
                    break;
                case PBEAMIndex_F1:
                    tempData.m_F1[0] = convertToReal (curStrKey);
                    break;
                case PBEAMIndex_F2:
                    tempData.m_F2[0] = convertToReal (curStrKey);
                    break;
                default:
                    break;
                }
            }
        }

        // 如果开始行数为2，则将SO字段的A端设为1.0
        if (beginRowSize == 2)
        {
            tempData.m_SO[0] = 1.0;
        }

        // 解析结束行的数据
        size_t const endBeginIndex = dividedEntryInfo.size () - endRowSize * 10;  // 计算结束行开始的位置
        for (size_t endRowIndex = 0; endRowIndex < endRowSize; endRowIndex++)
        {
            // 遍历每一行的字段
            for (int fieldsColumn = 1; fieldsColumn < 9; fieldsColumn++)
            {
                judgeEnum              = endRowIndex * 10 + fieldsColumn + 20;             // 计算当前枚举值
                const size_t curIndex  = endRowIndex * 10 + fieldsColumn + endBeginIndex;  // 当前字段的索引
                std::string& curStrKey = dividedEntryInfo[curIndex];                       // 当前字段的值
                bool const   isEmpty   = is_all_space (curStrKey);

                if (isEmpty)
                {
                    continue;  // 如果字段为空，跳过该字段
                }

                // 将当前字段转换为科学计数法表示

                // 根据枚举值解析字段并填充到tempData结构体中
                switch (judgeEnum)
                {
                case PBEAMIndex_K1:
                    tempData.m_K1 = convertToReal (curStrKey);
                    break;
                case PBEAMIndex_K2:
                    tempData.m_K2 = convertToReal (curStrKey);
                    break;
                case PBEAMIndex_S1:
                    tempData.m_S1 = convertToReal (curStrKey);
                    break;
                case PBEAMIndex_S2:
                    tempData.m_S2 = convertToReal (curStrKey);
                    break;
                case PBEAMIndex_NSIA:
                    tempData.m_NSIA = convertToReal (curStrKey);
                    break;
                case PBEAMIndex_NSIB:
                    tempData.m_NSIB = convertToReal (curStrKey);
                    break;
                case PBEAMIndex_CWA:
                    tempData.m_CWA = convertToReal (curStrKey);
                    break;
                case PBEAMIndex_CWB:
                    tempData.m_CWB = convertToReal (curStrKey);
                    break;
                case PBEAMIndex_M1A:
                    tempData.m_M1A = convertToReal (curStrKey);
                    break;
                case PBEAMIndex_M2A:
                    tempData.m_M2A = convertToReal (curStrKey);
                    break;
                case PBEAMIndex_M1B:
                    tempData.m_M1B = convertToReal (curStrKey);
                    break;
                case PBEAMIndex_M2B:
                    tempData.m_M2B = convertToReal (curStrKey);
                    break;
                case PBEAMIndex_N1A:
                    tempData.m_N1A = convertToReal (curStrKey);
                    break;
                case PBEAMIndex_N2A:
                    tempData.m_N2A = convertToReal (curStrKey);
                    break;
                case PBEAMIndex_N1B:
                    tempData.m_N1B = convertToReal (curStrKey);
                    break;
                case PBEAMIndex_N2B:
                    tempData.m_N2B = convertToReal (curStrKey);
                    break;
                default:
                    break;
                }
            }
        }

        // 如果B端的某些字段为0.0，使用A端的值替代
        if (tempData.m_NSIB == 0.0)
        {
            tempData.m_NSIB = tempData.m_NSIA;
        }
        if (tempData.m_CWB == 0.0)
        {
            tempData.m_CWB = tempData.m_CWA;
        }
        if (tempData.m_M1B == 0.0)
        {
            tempData.m_M1B = tempData.m_M1A;
        }
        if (tempData.m_M2B == 0.0)
        {
            tempData.m_M2B = tempData.m_M2A;
        }
        if (tempData.m_N1B == 0.0)
        {
            tempData.m_N1B = tempData.m_N1A;
        }
        if (tempData.m_N2B == 0.0)
        {
            tempData.m_N2B = tempData.m_N2A;
        }
    }
    /**
     * @brief 解析单个中间数据并填充到PBEAMData结构中，根据SO类型和是否为末端部分进行处理。
     *
     * @param dividedEntryInfo 分割后的输入数据，包含每一行的字段。
     * @param IntermediateDataInfo 当前中间数据的相关信息（包括SO类型和行索引等）。
     * @param arrIndex 要填充数据的数组索引。
     * @param isEndSection 是否为末端部分，若为true则处理B端数据。
     * @param tempData 用于存储解析结果的PBEAMData结构。
     */
    void parseSingleIntermediateData (std::vector<std::string>&          dividedEntryInfo,
                                      IntermediateData&                  IntermediateDataInfo,
                                      size_t                             arrIndex,
                                      bool                               isEndSection,
                                      SG::DataStructure::FEM::PBEAMData& tempData)
    {
        // “YESA” Stresses recovered at points with the same y and z location as end A.
        if (IntermediateDataInfo.soType == SOSTATUS_YESA)
        {
            tempData.m_C1[arrIndex] = tempData.m_C1[0];

            tempData.m_C2[arrIndex] = tempData.m_C2[0];

            tempData.m_D1[arrIndex] = tempData.m_D1[0];

            tempData.m_D2[arrIndex] = tempData.m_D2[0];

            tempData.m_E1[arrIndex] = tempData.m_E1[0];

            tempData.m_E2[arrIndex] = tempData.m_E2[0];

            tempData.m_F1[arrIndex] = tempData.m_F1[0];

            tempData.m_F2[arrIndex] = tempData.m_F2[0];
            if (isEndSection)
            {
                tempData.m_C1[10] = tempData.m_C1[0];

                tempData.m_C2[10] = tempData.m_C2[0];

                tempData.m_D1[10] = tempData.m_D1[0];

                tempData.m_D2[10] = tempData.m_D2[0];

                tempData.m_E1[10] = tempData.m_E1[0];

                tempData.m_E2[10] = tempData.m_E2[0];

                tempData.m_F1[10] = tempData.m_F1[0];

                tempData.m_F2[10] = tempData.m_F2[0];
            }
        }

        size_t judgeEnum = 0;  // 用于与枚举值做对比的变量
        for (size_t corRowOrder = 0; corRowOrder < IntermediateDataInfo.rowSize; corRowOrder++)
        {
            for (int fieldsColumn = 1; fieldsColumn < 9; fieldsColumn++)
            {
                judgeEnum              = corRowOrder * 10 + fieldsColumn;                                    // 计算当前枚举值
                const size_t curIndex  = (corRowOrder + IntermediateDataInfo.rowIndex) * 10 + fieldsColumn;  // 当前字段的索引
                std::string& curStrKey = dividedEntryInfo[curIndex];                                         // 获取当前字段
                bool const   isEmpty   = is_all_space (curStrKey);

                switch (judgeEnum)
                {
                case PBEAMVCSIndex_SO:
                    if (IntermediateDataInfo.soType != SOSTATUS_NO)
                    {
                        tempData.m_SO[arrIndex] = 1.0;
                    }
                    if (isEndSection)
                    {
                        tempData.m_SO[arrIndex] = tempData.m_SO[10] = 1.0;
                    }
                    break;
                case PBEAMVCSIndex_XXB:
                    if (isEndSection)
                    {
                        tempData.m_XXB[arrIndex] = tempData.m_XXB[10] = 1.0;
                    }
                    else
                    {
                        tempData.m_XXB[arrIndex] = IntermediateDataInfo.xbKey;
                    }

                    break;
                case PBEAMVCSIndex_A:
                    if (isEmpty)
                    {
                        // 空的B端值使用A端值
                        if (isEndSection)
                        {
                            tempData.m_A[arrIndex] = tempData.m_A[0];
                        }
                        // 空的非B端值
                        else
                        {
                            tempData.m_A[arrIndex] =
                                (tempData.m_A[10] - tempData.m_A[0]) * IntermediateDataInfo.xbKey + tempData.m_A[0];  // 使用插值确定
                        }
                    }
                    else
                    {
                        // 非空用词条值填充
                        tempData.m_A[arrIndex] = convertToReal (curStrKey);
                    }
                    // 如果是B端，需要更新数组最后一个元素
                    if (isEndSection)
                    {
                        tempData.m_A[10] = tempData.m_A[arrIndex];
                    }

                    break;
                case PBEAMVCSIndex_I1:
                    if (isEmpty)
                    {
                        if (isEndSection)
                        {
                            tempData.m_I1[arrIndex] = tempData.m_I1[0];
                        }
                        else
                        {
                            tempData.m_I1[arrIndex] =
                                (tempData.m_I1[10] - tempData.m_I1[0]) * IntermediateDataInfo.xbKey + tempData.m_I1[0];  // 使用插值确定
                        }
                    }
                    else
                    {
                        tempData.m_I1[arrIndex] = convertToReal (curStrKey);
                    }
                    if (isEndSection)
                    {
                        tempData.m_I1[10] = tempData.m_I1[arrIndex];
                    }
                    break;
                case PBEAMVCSIndex_I2:
                    if (isEmpty)
                    {
                        if (isEndSection)
                        {
                            tempData.m_I2[arrIndex] = tempData.m_I2[0];
                        }
                        else
                        {
                            tempData.m_I2[arrIndex] =
                                (tempData.m_I2[10] - tempData.m_I2[0]) * IntermediateDataInfo.xbKey + tempData.m_I2[0];  // 使用插值确定
                        }
                    }
                    else
                    {
                        tempData.m_I2[arrIndex] = convertToReal (curStrKey);
                    }
                    if (isEndSection)
                    {
                        tempData.m_I2[10] = tempData.m_I2[arrIndex];
                    }
                    break;
                case PBEAMVCSIndex_I12:
                    if (isEmpty)
                    {
                        if (isEndSection)
                        {
                            tempData.m_I12[arrIndex] = tempData.m_I12[0];
                        }
                        else
                        {
                            tempData.m_I12[arrIndex] =
                                (tempData.m_I12[10] - tempData.m_I12[0]) * IntermediateDataInfo.xbKey + tempData.m_I12[0];  // 使用插值确定
                        }
                    }
                    else
                    {
                        tempData.m_I12[arrIndex] = convertToReal (curStrKey);
                    }
                    if (isEndSection)
                    {
                        tempData.m_I12[10] = tempData.m_I12[arrIndex];
                    }
                    break;
                case PBEAMVCSIndex_J:
                    if (isEmpty)
                    {
                        if (isEndSection)
                        {
                            tempData.m_J[arrIndex] = tempData.m_J[0];
                        }
                        else
                        {
                            tempData.m_J[arrIndex] =
                                (tempData.m_J[10] - tempData.m_J[0]) * IntermediateDataInfo.xbKey + tempData.m_J[0];  // 使用插值确定
                        }
                    }
                    else
                    {
                        tempData.m_J[arrIndex] = convertToReal (curStrKey);
                    }
                    if (isEndSection)
                    {
                        tempData.m_J[10] = tempData.m_J[arrIndex];
                    }
                    break;
                case PBEAMVCSIndex_NSM:
                    if (isEmpty)
                    {
                        if (isEndSection)
                        {
                            tempData.m_NSM[arrIndex] = tempData.m_NSM[0];
                        }
                        else
                        {
                            tempData.m_NSM[arrIndex] =
                                (tempData.m_NSM[10] - tempData.m_NSM[0]) * IntermediateDataInfo.xbKey + tempData.m_NSM[0];  // 使用插值确定
                        }
                    }
                    else
                    {
                        tempData.m_NSM[arrIndex] = convertToReal (curStrKey);
                    }
                    if (isEndSection)
                    {
                        tempData.m_NSM[10] = tempData.m_NSM[arrIndex];
                    }
                    break;
                case PBEAMVCSIndex_C1:
                    if (isEmpty)
                    {
                        if (isEndSection)
                        {
                            tempData.m_C1[arrIndex] = tempData.m_C1[0];
                        }
                        else
                        {
                            tempData.m_C1[arrIndex] =
                                (tempData.m_C1[10] - tempData.m_C1[0]) * IntermediateDataInfo.xbKey + tempData.m_C1[0];  // 使用插值确定
                        }
                    }
                    else
                    {
                        tempData.m_C1[arrIndex] = convertToReal (curStrKey);
                    }
                    if (isEndSection)
                    {
                        tempData.m_C1[10] = tempData.m_C1[arrIndex];
                    }
                    break;
                case PBEAMVCSIndex_C2:
                    if (isEmpty)
                    {
                        if (isEndSection)
                        {
                            tempData.m_C2[arrIndex] = tempData.m_C2[0];
                        }
                        else
                        {
                            tempData.m_C2[arrIndex] =
                                (tempData.m_C2[10] - tempData.m_C2[0]) * IntermediateDataInfo.xbKey + tempData.m_C2[0];  // 使用插值确定
                        }
                    }
                    else
                    {
                        tempData.m_C2[arrIndex] = convertToReal (curStrKey);
                    }
                    if (isEndSection)
                    {
                        tempData.m_C2[10] = tempData.m_C2[arrIndex];
                    }
                    break;
                case PBEAMVCSIndex_D1:
                    if (isEmpty)
                    {
                        if (isEndSection)
                        {
                            tempData.m_D1[arrIndex] = tempData.m_D1[0];
                        }
                        else
                        {
                            tempData.m_D1[arrIndex] =
                                (tempData.m_D1[10] - tempData.m_D1[0]) * IntermediateDataInfo.xbKey + tempData.m_D1[0];  // 使用插值确定
                        }
                    }
                    else
                    {
                        tempData.m_D1[arrIndex] = convertToReal (curStrKey);
                    }
                    if (isEndSection)
                    {
                        tempData.m_D1[10] = tempData.m_D1[arrIndex];
                    }
                    break;
                case PBEAMVCSIndex_D2:
                    if (isEmpty)
                    {
                        if (isEndSection)
                        {
                            tempData.m_D2[arrIndex] = tempData.m_D2[0];
                        }
                        else
                        {
                            tempData.m_D2[arrIndex] =
                                (tempData.m_D2[10] - tempData.m_D2[0]) * IntermediateDataInfo.xbKey + tempData.m_D2[0];  // 使用插值确定
                        }
                    }
                    else
                    {
                        tempData.m_D2[arrIndex] = convertToReal (curStrKey);
                    }
                    if (isEndSection)
                    {
                        tempData.m_D2[10] = tempData.m_D2[arrIndex];
                    }
                    break;
                case PBEAMVCSIndex_E1:
                    if (isEmpty)
                    {
                        if (isEndSection)
                        {
                            tempData.m_E1[arrIndex] = tempData.m_E1[0];
                        }
                        else
                        {
                            tempData.m_E1[arrIndex] =
                                (tempData.m_E1[10] - tempData.m_E1[0]) * IntermediateDataInfo.xbKey + tempData.m_E1[0];  // 使用插值确定
                        }
                    }
                    else
                    {
                        tempData.m_E1[arrIndex] = convertToReal (curStrKey);
                    }
                    if (isEndSection)
                    {
                        tempData.m_E1[10] = tempData.m_E1[arrIndex];
                    }
                    break;
                case PBEAMVCSIndex_E2:
                    if (isEmpty)
                    {
                        if (isEndSection)
                        {
                            tempData.m_E2[arrIndex] = tempData.m_E2[0];
                        }
                        else
                        {
                            tempData.m_E2[arrIndex] =
                                (tempData.m_E2[10] - tempData.m_E2[0]) * IntermediateDataInfo.xbKey + tempData.m_E2[0];  // 使用插值确定
                        }
                    }
                    else
                    {
                        tempData.m_E2[arrIndex] = convertToReal (curStrKey);
                    }
                    if (isEndSection)
                    {
                        tempData.m_E2[10] = tempData.m_E2[arrIndex];
                    }
                    break;
                case PBEAMVCSIndex_F1:
                    if (isEmpty)
                    {
                        if (isEndSection)
                        {
                            tempData.m_F1[arrIndex] = tempData.m_F1[0];
                        }
                        else
                        {
                            tempData.m_F1[arrIndex] =
                                (tempData.m_F1[10] - tempData.m_F1[0]) * IntermediateDataInfo.xbKey + tempData.m_F1[0];  // 使用插值确定
                        }
                    }
                    else
                    {
                        tempData.m_F1[arrIndex] = convertToReal (curStrKey);
                    }
                    if (isEndSection)
                    {
                        tempData.m_F1[10] = tempData.m_F1[arrIndex];
                    }
                    break;
                case PBEAMVCSIndex_F2:
                    if (isEmpty)
                    {
                        if (isEndSection)
                        {
                            tempData.m_F2[arrIndex] = tempData.m_F2[0];
                        }
                        else
                        {
                            tempData.m_F2[arrIndex] =
                                (tempData.m_F2[10] - tempData.m_F2[0]) * IntermediateDataInfo.xbKey + tempData.m_F2[0];  // 使用插值确定
                        }
                    }
                    else
                    {
                        tempData.m_F2[arrIndex] = convertToReal (curStrKey);
                    }
                    if (isEndSection)
                    {
                        tempData.m_F2[10] = tempData.m_F2[arrIndex];
                    }
                    break;
                default:
                    break;
                }
            }
        }
    }

    /**
     * @brief 解析中间数据并填充到PBEAMData结构中，判断截面属性是否相同并设置相关标志位。
     *
     * @param dividedEntryInfo 分割后的输入数据，包含每一行的字段。
     * @param IntermediateDataSumInfo 中间数据的总结信息，包含多个IntermediateData实例。
     * @param tempData 用于存储解析结果的PBEAMData结构。
     */
    void parseIntermediateData (std::vector<std::string>&          dividedEntryInfo,
                                std::map<double, IntermediateData> IntermediateDataSumInfo,
                                SG::DataStructure::FEM::PBEAMData& tempData)
    {
        // 初始化索引，arrIndex代表当前填充的数组索引，0代表A端面
        size_t const arrIndex = 1;

        // 迭代遍历中间数据
        for (auto iter = IntermediateDataSumInfo.rbegin (); iter != IntermediateDataSumInfo.rend (); iter++)
        {
            // 如果是第一项，表示末端B段
            if (iter == IntermediateDataSumInfo.rbegin ())
            {
                parseSingleIntermediateData (dividedEntryInfo, iter->second, arrIndex, true, tempData);
            }
            // 否则，处理非末端的数据
            else
            {
                parseSingleIntermediateData (dividedEntryInfo, iter->second, arrIndex, false, tempData);
            }
        }

        // 设置A端和B端的索引
        size_t const secA_Index = 0;
        size_t const secB_Index = 10;

        // 判断截面属性，若中间数据数量小于等于1，表示只有A和B端
        if (IntermediateDataSumInfo.size () <= 1)
        {
            tempData.m_nsges = 1;  // 只有一个截面
            // 检查A端与B端的截面属性是否相同，如果相同则m_ccf设为1，否则设为2
            if (tempData.m_A[secA_Index] == tempData.m_A[secB_Index] && tempData.m_I1[secA_Index] == tempData.m_I1[secB_Index]
                && tempData.m_I2[secA_Index] == tempData.m_I2[secB_Index] && tempData.m_I12[secA_Index] == tempData.m_I12[secB_Index]
                && tempData.m_J[secA_Index] == tempData.m_J[secB_Index] && tempData.m_NSM[secA_Index] == tempData.m_NSM[secB_Index]
                && tempData.m_C1[secA_Index] == tempData.m_C1[secB_Index] && tempData.m_C2[secA_Index] == tempData.m_C2[secB_Index]
                && tempData.m_D1[secA_Index] == tempData.m_D1[secB_Index] && tempData.m_D2[secA_Index] == tempData.m_D2[secB_Index]
                && tempData.m_E1[secA_Index] == tempData.m_E1[secB_Index] && tempData.m_F1[secA_Index] == tempData.m_F1[secB_Index]
                && tempData.m_F2[secA_Index] == tempData.m_F2[secB_Index])
            {
                tempData.m_ccf = 1;  // A端与B端截面相同
            }
            else
            {
                tempData.m_ccf = 2;  // A端与B端截面不同
            }
        }
        else
        {
            // 中间数据数量大于1，表示有多个截面
            tempData.m_nsges = IntermediateDataSumInfo.size ();  // 设置截面数量
            tempData.m_ccf   = 0;                                // m_ccf设为0，
        }
    }
}  // namespace

Status BDF::BULKDATA::ParsePBEAM (const std::shared_ptr<SG::DBManager::DBServiceFactory>& dbServiceFactory,
                                  std::vector<std::string>&                               dividedEntryInfo)
{
    // 创建临时数据对象以存储解析结果
    SG::DataStructure::FEM::PBEAMData tempData;

    // TODO(bug 001) 需要正确填写m_domainId
    tempData.m_domainId = 1;

    size_t const                       rowNum       = dividedEntryInfo.size () / 10;
    size_t                             endRowSize   = 0;  // 用于记录末尾的行数
    size_t                             beginRowSize = 0;  // 记录初始行数
    std::map<double, IntermediateData> IntermediateDataSumInfo;
    summarizeData (dividedEntryInfo, rowNum, IntermediateDataSumInfo, beginRowSize, endRowSize);

    // 解析常规行数，首先处理常规数据
    parseGeneralData (dividedEntryInfo, beginRowSize, endRowSize, tempData);

    // 解析中间行，即变截面数据
    if (IntermediateDataSumInfo.size () == 0)
    {
        // 没有中间截面数据，A端面=B端面
        tempData.m_SO[10]  = tempData.m_SO[0];
        tempData.m_XXB[10] = 1.0;
        tempData.m_A[10]   = tempData.m_A[0];
        tempData.m_I1[10]  = tempData.m_I1[0];
        tempData.m_I2[10]  = tempData.m_I2[0];
        tempData.m_I12[10] = tempData.m_I12[0];
        tempData.m_J[10]   = tempData.m_J[0];
        tempData.m_NSM[10] = tempData.m_NSM[0];
        tempData.m_C1[10]  = tempData.m_C1[0];
        tempData.m_C2[10]  = tempData.m_C2[0];

        tempData.m_D1[10] = tempData.m_D1[0];
        tempData.m_D2[10] = tempData.m_D2[0];
        tempData.m_E1[10] = tempData.m_E1[0];
        tempData.m_E2[10] = tempData.m_E2[0];
        tempData.m_F1[10] = tempData.m_F1[0];
        tempData.m_F2[10] = tempData.m_F2[0];
        tempData.m_ccf    = 1;
        tempData.m_nsges  = 1;
    }
    else
    {
        parseIntermediateData (dividedEntryInfo, IntermediateDataSumInfo, tempData);
    }

    // 将解析后的数据添加到数据库中
    if (!tempData.Check ())
    {
        SGError << "check PBEAM data, id == " << tempData.m_id;
        return Status::STATUS_Failure;
    }
    return dbServiceFactory->get<IPBEAMService> ()->Add (tempData);
}